/**
 * 文件名：UserMgrController.java
 *
 * 创建人：虞云波 - .com
 *
 * 创建时间：2017年10月31日 下午5:07:52
 *
 * 版权所有：江苏晟邦网络科技有限公司
 */
package com.cbwl.eoas.web.controller.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cbwl.eoas.api.client.service.sysconfig.ISmsMessageApi;
import com.cbwl.eoas.cms.api.sysconfig.ISysUserService;
import com.cbwl.eoas.cms.model.eoas.AppSmsLogDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysResourceDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.common.cache.redis.RedisCache;
import com.cbwl.eoas.common.util.Md5Util;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;
import com.cbwl.eoas.common.web.Result;
import com.cbwl.eoas.web.framework.annotation.InterceptorUrl;

/**
 * <p>[描述信息：用户管理Controller]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月31日 下午5:07:52
 */
@RestController
@RequestMapping("/v1/user")
public class UserMgrController {

	
	@Autowired
	private ISysUserService sysUserService;
	
	
	@Autowired
	private RedisCache<String,String> redisCache;
	
	
	@Autowired
	private ISmsMessageApi ismsMessageApi;
	
	
	/**
	 * 
	 * <p>根据用户id查询出菜单信息</p>
	 * 
	 * @param tokenId
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年10月31日 下午5:16:17
	 */
	@RequestMapping(value="/getMenu",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object getMenu(HttpServletRequest request,String tokenId){
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(tokenId)){
			result.setCode("CBWL-000010");
			return result;
		}
		
		String uid = redisCache.getStr(tokenId);
		SysResourceDTO bean = new SysResourceDTO();
		bean.setUid(Long.valueOf(uid));
		result.setData(sysUserService.queryObjectListByUid(bean));
		return result;
	}
	
	
	/**
	 * 
	 * <p>用户修改密码</p>
	 * 
	 * @param tokenId
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @throws Exception 
	 * @date: Created on 2017年11月2日 上午10:53:51
	 */
	@RequestMapping(value="/updatePassWord",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object updatePassWord(HttpServletRequest request,SysUserDTO bean,String newUserPassword) throws Exception{
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		String userPassword = bean.getUserPassword();
		if(StringUtils.isEmpty(bean.getTokenId()) || StringUtils.isEmpty(bean.getUserPassword())){
			result.setCode("CBWL-000001");
			return result;
		}
		String uid = redisCache.getStr(bean.getTokenId());
		bean.setId(Long.valueOf(uid));
		bean = sysUserService.getObjectById(bean);
		
		if(!bean.getUserPassword().equals(Md5Util.encodePassword(userPassword,bean.getUserJobno()))){
			result.setCode("CBWL-000015");
			return result;
		}
		bean.setUserPassword(newUserPassword);
		bean.setGmtModified(new Date());
		bean.setModifier(Long.valueOf(uid));
		sysUserService.updateUserInfo(bean);
		
		return result;
	}
	
	
	/**
	 * 
	 * <p>获取验证码</p>
	 * 
	 * @param request
	 * @param bean
	 * @param userBean
	 * @return
	 * @throws Exception
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年12月4日 上午10:23:21
	 */
	@RequestMapping(value="/getCode",method = RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object getCode(HttpServletRequest request,AppSmsLogDTO bean,SysUserDTO userBean) throws Exception{
		
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getSmsTelephone())  && StringUtils.isEmpty(userBean.getTokenId())){
			result.setCode("CBWL-000010");
			return result;
		}
		
		bean.setSmsSrc(DicDetailConstant.sms_src_1);
		bean.setSelectType("2");
		String uid = redisCache.getStr(userBean.getTokenId());
		bean.setCreater((Long.valueOf(uid)));
		bean.setGmtCreate(new Date());
		
		
		//发送短信验证码
		return ismsMessageApi.saveAndSendMessage(bean,userBean);
	}
	
	
	
	/**
	 * 
	 * <p>修改手机号码</p>
	 * 
	 * @param bean
	 * @param newUserPassword
	 * @return
	 * @throws Exception
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月2日 上午11:17:37
	 */
	@RequestMapping(value="/updatePhone",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object updatePhone(HttpServletRequest request,SysUserDTO bean,AppSmsLogDTO smsBean) throws Exception{
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getTokenId()) || StringUtils.isEmpty(bean.getUserPhone())){
			result.setCode("CBWL-000001");
			return result;
		}
		//比对验证码是正确
		smsBean.setSmsSrc(DicDetailConstant.sms_src_1);
		smsBean.setSelectType("1");
		Result smsResult = ismsMessageApi.checkCode(smsBean);
		if(!"200".equals(smsResult.getCode())){
			return result;
		}
		String uid = redisCache.getStr(bean.getTokenId());
		bean.setId(Long.valueOf(uid));
		bean = sysUserService.getObjectById(bean);
		bean.setGmtModified(new Date());
		bean.setModifier(Long.valueOf(uid));
		sysUserService.updateUserInfo(bean);
		
		return result;
	}
	
	
	/**
	 * 
	 * <p>用户上传头像</p>
	 * 
	 * @param request
	 * @param bean
	 * @return
	 * @throws Exception
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年12月4日 上午11:02:26
	 */
	@RequestMapping(value="/uploadEmpAvatar",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object uploadEmpAvatar(HttpServletRequest request,SysUserDTO bean) throws Exception{
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getTokenId()) || StringUtils.isEmpty(bean.getEmpAvatar())){
			result.setCode("CBWL-000010");
			return result;
		}
		//更新用户头像
		String uid = redisCache.getStr(bean.getTokenId());
		bean.setId(Long.valueOf(uid));
		long resInt = sysUserService.updateUserInfo(bean);
		if(resInt<=0){
			result.setCode("CBWL-000017");
			return result;
		}		
		return result;
	}
	
	
	
}
